package com.tenebrous.jestic.controller.framwork.meber.author;

import com.tenebrous.jestic.compents.annotations.Weblog;
import com.tenebrous.jestic.compents.annotations.WeblogEnum;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.framwork.meber.author.AuthGroup;
import com.tenebrous.jestic.service.framwork.meber.author.AuthGroupService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Validated
@RestController
@RequestMapping("/author/group")
public class GroupController extends SupperController {

    @Autowired
    private AuthGroupService groupService;

    @PostMapping("/get")
    public ApiResult get(@NotBlank String id) {
        AuthGroup item = groupService.selectByPrimaryKey(id);
        if (item != null) {
            return new ApiResult(200, "查询成功。", item);
        }
        return new ApiResult(999, "查询失败，未找到指定对象。");
    }

    /**
     * 分页查询群组
     *
     * @param categoryid
     * @param name
     * @return
     */
    @PostMapping("/paging")
    public ApiResult paging(String categoryid, String name) {
        return new ApiResult(200, "查询成功。", groupService.queryPagingList(super.getPageIndex(), super.getPageSize(), categoryid, name));
    }

    /**
     * 分页查询群组用户集合
     *
     * @param gid
     * @return
     */
    @PostMapping("/paging-group-users")
    public ApiResult pagingGroupUsers(@NotBlank String gid, String realname) {
        return new ApiResult(200, "查询成功。", groupService.queryPagingGroupUserList(super.getPageIndex(), super.getPageSize(), gid, realname));
    }

    /**
     * 查询群组用户集合
     *
     * @param gid
     * @return
     */
    @PostMapping("/query-group-users")
    public ApiResult queryGroupUsers(@NotBlank String gid) {
        return new ApiResult(200, "查询成功。", groupService.queryGroupUserList(gid));
    }

    /**
     * 更新群组信息
     *
     * @param item
     * @return
     */
    @PostMapping("/update")
    @Weblog(description = "更新群组信息", method = WeblogEnum.UPDATE)
    public ApiResult update(@NotNull AuthGroup item) {
        if (StringUtils.isNotBlank(item.getId())) {
            item.setUpdateDate(new Date());
            groupService.updateSelective(item);
        } else {
            item.setId(UUIDUtils.randomUUID());
            item.setCreateDate(new Date());
            groupService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。");
    }

    /**
     * 更新群组用户信息
     *
     * @return
     */
    @PostMapping("/insert-group-users")
    @Weblog(description = "更新群组用户信息", method = WeblogEnum.UPDATE)
    public ApiResult insertGroupUsers(@NotBlank String gid, @NotEmpty String[] uids) {
        return new ApiResult(200, "更新成功。", groupService.insertGroupUsers(gid, uids));
    }

    /**
     * 删除群组记录
     *
     * @return
     */
    @PostMapping("/delete")
    @Weblog(description = "删除群组记录", method = WeblogEnum.DELETE)
    public ApiResult delete(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", groupService.deleteByIds(ids));
    }

    /**
     * 删除群组用户记录
     *
     * @return
     */
    @PostMapping("/delete-group-users")
    @Weblog(description = "删除群组用户记录", method = WeblogEnum.DELETE)
    public ApiResult deleteGroupUsers(@NotBlank String gid, @NotEmpty String[] uids) {
        return new ApiResult(200, "删除成功。", groupService.deleteGroupUsers(gid, uids));
    }

}
